package com.lanou.beautifulwardrobe.brand.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.activity.BrandShopActivity;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.RiseItem;

import java.util.List;

/**
 * Created by dllo on 15/10/30.
 */
public class BrandRankAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<RiseItem> datas;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private Context context;
    private ImageLoader imageLoader;

    //    private MyHotlistItemClickListener listener;
//    public void setOnMyHotlistItemClickListener(MyHotlistItemClickListener listener){
//        this.listener=listener;
//    }
    public BrandRankAdapter(Context c) {
        this.context = c;
    }

    public void addData(List<RiseItem> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case 0:
                View firView = LayoutInflater.from(parent.getContext()).inflate(R.layout.brandfirranking_item, null);
                holder = new FirRankViewHolder(firView);
                break;
            default:
                View secView = LayoutInflater.from(parent.getContext()).inflate(R.layout.brandsecranking_item, null);
                holder = new SecRankViewHolder(secView);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (position) {
            case 0:
                FirRankViewHolder firHolder = (FirRankViewHolder) holder;
                if (datas != null && datas.size() > 0) {
                    RiseItem item = datas.get(position);
                    firHolder.firPosition=position;
                    firHolder.fir_style.setText(item.getStyle());
                    firHolder.fir_description.setText(item.getDescription());
                    firHolder.fir_likes.setText(item.getLikes_count());
                    getBitMap(item.getTaobao_pic_urls().get(0), firHolder.fir_firImage);
                    getBitMap(item.getTaobao_pic_urls().get(1), firHolder.fir_secImage);
                    getBitMap(item.getTaobao_pic_urls().get(2), firHolder.fir_tirImage);
                    getBitMap(item.getLogo_url(), firHolder.fir_logoImage);
                }
                break;
            default:
                SecRankViewHolder secHolder = (SecRankViewHolder) holder;
                if (datas != null && datas.size() > 0) {
                    RiseItem item = datas.get(position);
                    secHolder.secPosition=position;
                    secHolder.sec_style.setText(item.getStyle());
                    secHolder.sec_description.setText(item.getDescription());
                    secHolder.sec_likes.setText(item.getLikes_count());
                    getBitMap(item.getTaobao_pic_urls().get(0), secHolder.sec_image);
                    getBitMap(item.getLogo_url(), secHolder.sec_logoImage);
                }
                break;
        }
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    public static final int TYPE0 = 0;
    public static final int TYPE1 = 1;

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE0;
        } else
            return TYPE1;
    }

    class FirRankViewHolder extends RecyclerView.ViewHolder  {
        private TextView fir_likes, fir_description, fir_style;
        private NetworkImageView fir_firImage, fir_secImage, fir_tirImage, fir_logoImage;
        private LinearLayout firstRankLayout;
        private int firPosition;

        public FirRankViewHolder(View view) {
            super(view);
            fir_likes = (TextView) view.findViewById(R.id.fir_likes);
            fir_description = (TextView) view.findViewById(R.id.fir_description);
            fir_style = (TextView) view.findViewById(R.id.fir_style);
            fir_firImage = (NetworkImageView) view.findViewById(R.id.fir_firImage);
            fir_secImage = (NetworkImageView) view.findViewById(R.id.fir_secImage);
            fir_tirImage = (NetworkImageView) view.findViewById(R.id.fir_tirImage);
            fir_logoImage = (NetworkImageView) view.findViewById(R.id.fir_logoImage);

            firstRankLayout = (LinearLayout) view.findViewById(R.id.firstRankLayout);
            firstRankLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(context, BrandShopActivity.class);
                    long id = datas.get(firPosition).getId();
                    String imageUrl = datas.get(firPosition).getLogo_url();
                    String title = datas.get(firPosition).getTitle();
                    intent.putExtra("id", id + "");
                    intent.putExtra("title", title);
                    intent.putExtra("imageUrl", imageUrl);
                    context.startActivity(intent);
                }
            });
        }



    }

    class SecRankViewHolder extends RecyclerView.ViewHolder {
        private TextView sec_likes, sec_description, sec_style;
        private NetworkImageView sec_image, sec_logoImage;
        private  LinearLayout secRankLayout;
        private int secPosition;

        public SecRankViewHolder(View view) {
            super(view);
            sec_likes = (TextView) view.findViewById(R.id.sec_likes);
            sec_description = (TextView) view.findViewById(R.id.sec_description);
            sec_style = (TextView) view.findViewById(R.id.sec_style);
            sec_image = (NetworkImageView) view.findViewById(R.id.sec_image);
            sec_logoImage = (NetworkImageView) view.findViewById(R.id.sec_logoImage);

            secRankLayout= (LinearLayout) view.findViewById(R.id.secRankLayout);
            secRankLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, BrandShopActivity.class);
                    long id = datas.get(secPosition).getId();
                    String imageUrl = datas.get(secPosition).getLogo_url();
                    String title = datas.get(secPosition).getTitle();
                    intent.putExtra("id", id + "");
                    intent.putExtra("title", title);
                    intent.putExtra("imageUrl", imageUrl);
                    context.startActivity(intent);
                }
            });
        }
    }

    /** 拉取图片 **/
    private void getBitMap(String url, NetworkImageView imageView){
        imageLoader = volleySingleton.getImageLoader();
        imageView.setDefaultImageResId(R.mipmap.ic_launcher);
        imageView.setErrorImageResId(R.mipmap.ic_launcher);
        imageView.setImageUrl(url, imageLoader);
    }
}
